package cn.groupmarket.domain.activity.service.trial.node;

import cn.groupmarket.domain.activity.model.entity.MarketProductEntity;
import cn.groupmarket.domain.activity.model.entity.TrialBalanceEntity;
import cn.groupmarket.domain.activity.model.valobj.DiscountTypeEnum;
import cn.groupmarket.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import cn.groupmarket.domain.activity.model.valobj.SkuVO;
import cn.groupmarket.domain.activity.service.discount.IDiscountCalculateService;
import cn.groupmarket.domain.activity.service.trial.AbstractGroupBuyMarketSupport;
import cn.groupmarket.domain.activity.service.trial.factory.DefaultActivityStrategyFactory;
import cn.groupmarket.domain.activity.service.trial.thread.QueryGroupBuyActivityDiscountVOThreadTask;
import cn.groupmarket.domain.activity.service.trial.thread.QuerySkuVOFromDBThreadTask;
import cn.groupmarket.types.design.framework.tree.StrategyHandler;
import cn.groupmarket.types.enums.ResponseCode;
import cn.groupmarket.types.exception.AppException;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;
import java.util.concurrent.*;

@Slf4j
@Service
public class MarketNode extends AbstractGroupBuyMarketSupport<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> {

    @Resource
    private ThreadPoolExecutor threadPoolExecutor;
    @Resource
    private EndNode endNode;
    @Resource
    private Map<String, IDiscountCalculateService> discountCalculateServiceMap;
    @Resource
    private ErrorNode errorNode;

    @Override
    public TrialBalanceEntity doApply(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        log.info("拼图商品查询试算服务-MarketNode userId:{} requestParameter:{}",requestParameter.getUserId(), JSON.toJSONString(requestParameter));
        GroupBuyActivityDiscountVO groupBuyActivityDiscountVO = dynamicContext.getGroupBuyActivityDiscountVO();
        if(null == groupBuyActivityDiscountVO){
            return router(requestParameter,dynamicContext);
        }
        GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount = groupBuyActivityDiscountVO.getGroupBuyDiscount();
        SkuVO skuVO = dynamicContext.getSkuVO();
        if(null == skuVO){
            return router(requestParameter,dynamicContext);
        }
        IDiscountCalculateService discountCalculateService = discountCalculateServiceMap.get(groupBuyDiscount.getMarketPlan());
        if(discountCalculateService == null) {
            log.info("不存在{}类型的折扣计算服务，支持类型为:{}",groupBuyDiscount.getMarketPlan(),JSON.toJSONString(discountCalculateServiceMap.keySet()));
            throw new AppException(ResponseCode.E0001.getCode(),ResponseCode.E0001.getInfo());
        }
        BigDecimal payPrice = discountCalculateService.calculate(requestParameter.getUserId(), skuVO.getOriginalPrice(), groupBuyDiscount);
        dynamicContext.setPayPrice(payPrice);
        dynamicContext.setDeductionPrice(skuVO.getOriginalPrice().subtract(payPrice));
        return router(requestParameter,dynamicContext);
    }

    @Override
    public StrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> get(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) {
        if(null == dynamicContext.getGroupBuyActivityDiscountVO() || null == dynamicContext.getSkuVO()) {
            return errorNode;
        }
        return endNode;
    }
}
